<?php

if(isset($_GET['xoa'])){
    
    if(!empty($_GET['mahd'])){
        $mahd = $_GET['mahd'];
        mysql_query("DELETE FROM tbl_hoidongthi WHERE ma_hoidong = '$mahd'");
        header('Location:index.php?muc=tochucthi&modul=laphoidongthi');
    } 
}
if(isset($_POST['sua'])){
    if(!empty($_POST['sohd'])){
        $i = $_POST['sohd'];
       if(!empty($_POST['donvian'.$i]) && !empty($_POST['hocvuan'.$i]) && !empty($_POST['ngayan'.$i]) && !empty($_POST['hoidongan'.$i])){
            $donvian = $_POST['donvian'.$i];
            $hocvuan = $_POST['hocvuan'.$i];
            $ngayan = $_POST['ngayan'.$i];
            $hoidongan = $_POST['hoidongan'.$i];
            mysql_query("UPDATE tbl_hoidongthi SET ma_donvi = '$donvian', ma_donvihocvu = '$hocvuan', thoigian_hoidong = '$ngayan' WHERE ma_hoidong = '$hoidongan'");
       }
    }
}

else if(isset($_POST['luu'])){
    if(!empty($_POST['donvi']) && !empty($_POST['hocvu']) && !empty($_POST['ngay'])){
    $donvi = $_POST['donvi'];
    $hocvu = $_POST['hocvu'];
    $ngay = $_POST['ngay'];
    $mahd = "HD".$donvi.$hocvu.str_replace("/", "", $_POST['ngay']);
    mysql_query("INSERT INTO tbl_hoidongthi VALUES('$mahd', '$hocvu', '$donvi', '$ngay')");
    }
}
include '../lib/BLL/DonViBLL.php';
include '../lib/BLL/DmNganhBLL.php';
include '../lib/BLL/DmMonHocBLL.php';
include '../lib/BLL/DonViHocVuBLL.php';
$dv = new DonViBLL();
$nganh = new DmNganhBLL();
$mh = new DmMonHocBLL();
$dvhv = new DonViHocVuBLL();

$ds_nganh = $nganh->getListNganh();
$ds_dv = $dv->getListDonVi_Lienket();
$ds_mh = $mh->getListDmMonHoc();
$ds_dvhv = $dvhv->getListDonViHocVu();

$sql = "SELECT hd.ma_hoidong, hd.ma_donvihocvu, hd.thoigian_hoidong, dv.ten_donvi, dv.ma_donvi FROM tbl_hoidongthi AS hd
        INNER JOIN tbl_donvi AS dv WHERE hd.ma_donvi = dv.ma_donvi";
$listhdthi = $db->getList($sql);

$smarty->assign('listhdthi', $listhdthi);
$smarty->assign('ds_mh',$ds_mh);
$smarty->assign('ds_dv',$ds_dv);
$smarty->assign('ds_nganh',$ds_nganh);
$smarty->assign('ds_dvhv',$ds_dvhv);
$smarty->display('tochucthi/laphoidongthi.tpl');
?>